﻿using AutoBlogger.Properties;
using AutoBlogger.Tool;
using AutoBlogger.View;
using HtmlAgilityPack;
using JoeBlogs;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace AutoBlogger.DB
{
    internal class MongoDbHelper
    {
        static MongoClient client;
        static MongoServer server;
        static MongoDatabase database;

        static MongoDbHelper()
        {
            client = new MongoClient(Settings.Default.ConnectionString);
            server = client.GetServer();
            database = server.GetDatabase(Settings.Default.FoodBlogs);
        }

        internal static void AddBlog(BlogModel blog)
        {
            if (blog == null || string.IsNullOrEmpty(blog.Title) || string.IsNullOrEmpty(blog.Content))
            {
                return;
            }
            var collection = database.GetCollection<BlogModel>(Settings.Default.EnglishBlogs);
            collection.Insert(blog);
        }

        internal static void PublishBlog(Label lblControl)
        {
            WordPressWrapper wpWrapper = new WordPressWrapper("http://www.liuwaner.net/xmlrpc.php", "lbp0200", "xgn2207", 1);

            var collection = database.GetCollection<BlogModel>(Settings.Default.EnglishBlogs);

            while (MainForm.IsGo)
            {
                var query = collection.AsQueryable<BlogModel>().Where(e => e.IsPosted != true).Select(e => e).Take(30);
                MainForm.IsGo = query.Count() > 0;
                foreach (var item in query)
                {
                    WordPressClient wpClient = new WordPressClient(wpWrapper);
                    var post_id = wpClient.NewPost(item);

                    if (post_id > 0)
                    {
                        item.IsPosted = true;
                        collection.Save(item);
                        lblControl.Text = item.Title;
                    }
                }
            }
        }

        internal static BlogModel TranslateChineseBlog()
        {
            var collection = database.GetCollection<BlogModel>(Settings.Default.EnglishBlogs);
            var noTransBlogs = from cBlogs in collection.AsQueryable<BlogModel>()
                               select cBlogs;
            foreach (var cBlog in noTransBlogs)
            {
                string translatedBlog = Tool.NetClient.TranslateText(cBlog.Title + "\n" + cBlog.Content);
            }

            return new BlogModel(string.Empty);
        }

        internal static bool IsExisting(uint id)
        {
            var collection = database.GetCollection<BlogModel>(Settings.Default.EnglishBlogs);
            if (collection.FindOneById(id) == null)
            {
                return false;
            }
            else
                return true;
        }
    }
}
